package com.je.app.control;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.UIManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class EnvioMail extends Thread{

	private String asunto;
	private String cuerpo;
	private List <String> destinatarios;
	
	public EnvioMail(String asunto, String cuerpo, List <String> destinatarios) {
		this.asunto=asunto;
		this.cuerpo=cuerpo;
		this.destinatarios=destinatarios;
	}
	
	@Override
	public void run(){

		Properties properties = System.getProperties();
		 
		 try{
			 properties.load(new FileInputStream("src/main/resources/mail.properties"));
	
			 Session session = Session.getDefaultInstance(properties);
	
			 MimeMessage message = new MimeMessage(session);

			 message.setFrom(new InternetAddress(properties.getProperty("user")));
			 message.setSubject(asunto);
			 message.setText(cuerpo);
			
			 Transport t =session.getTransport("smtp");
			 t.connect(properties.getProperty("host"),properties.getProperty("user"),properties.getProperty("password"));
			
			 message.addRecipient(Message.RecipientType.TO, new InternetAddress(properties.getProperty("to")));
		
			 t.sendMessage(message,message.getAllRecipients());
		 
		 }catch (MessagingException mex) {
	         mex.printStackTrace();
		 } catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
